﻿using MySql.Data.MySqlClient;
using SqlSugar;
using System.Data;
using System.Reflection;

namespace Finance.Api.Read.Extensions
{
    public static class ServiceCollectionExtensions
    {
        public static IServiceCollection AddServices(this IServiceCollection services, WebApplicationBuilder builder)
        {
            services.AddScoped<ISqlSugarClient>(s =>
            {
                //Scoped用SqlSugarClient 
                SqlSugarClient sqlSugar = new SqlSugarClient(new ConnectionConfig()
                {
                    DbType = SqlSugar.DbType.MySql,
                    ConnectionString = builder.Configuration.GetConnectionString("conn"),
                    IsAutoCloseConnection = true,
                });
                return sqlSugar;
            });
            services.AddMediatR(x => x.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));
            services.AddAutoMapper(typeof(MappingProfile));
            

            return services;
        }

        public static IServiceCollection DapperHelper(this IServiceCollection services,WebApplicationBuilder builder)
        {
            // 注册IDbConnection，使用MySQL连接
            services.AddScoped<IDbConnection>(s =>
            {
                var connection = new MySqlConnection(builder.Configuration.GetConnectionString("conn"));
                connection.Open();
                return connection;
            });
            return services;
        }
    }
}
